package com.usian.behavior.kafka;

import com.alibaba.fastjson.JSON;
import com.usian.behavior.service.BehaviorFollowService;
import com.usian.common.contants.behavior.BehaviorConstants;
import com.usian.model.behavior.dots.FollowBehaviorDto;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import java.util.Optional;

@Component
public class FollowBehaviorListern {
    @Autowired
    private BehaviorFollowService followService;

    @KafkaListener(topics = BehaviorConstants.LOGIN_BEHAVIOR_TOPIC)
    public void receiveMessage1(ConsumerRecord<?,?> record){
        Optional<? extends ConsumerRecord<?, ?>> optional = Optional.ofNullable(record);
        if(optional.isPresent()){
            String value = (String) record.value();
            followService.loginBehavior(Integer.parseInt(value));
        }
    }

    @KafkaListener(topics = BehaviorConstants.FOLLOW_BEHAVIOR_TOPIC)
    public void receiveMessage2(ConsumerRecord<?,?> record){
        Optional<? extends ConsumerRecord<?, ?>> optional = Optional.ofNullable(record);
        if(optional.isPresent()){
            String value = (String) record.value();
            FollowBehaviorDto followBehaviorDto = JSON.parseObject(value, FollowBehaviorDto.class);
            followService.followBehavior(followBehaviorDto);
        }
    }
}
